/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.testingProject.view;

import javax.swing.JOptionPane;
import org.colswe.testingProject.control.CRUDUsersControl;
import org.colswe.testingProject.control.PersonControl;
import org.colswe.testingProject.entity.Person;

/**
 *
 * @author Leonardo Bohórquez
 */
public class AdminUserPanel extends javax.swing.JPanel {

    private CRUDUsersControl cruduc;
    /**
     * Creates new form AdminUserPanel
     */
    private EditUserPanel pnlEditUserPanel;
    private PersonControl pc;

    public AdminUserPanel() {
        initComponents();
        cruduc = new CRUDUsersControl();
        pc = new PersonControl();
        tblUsers.setModel(cruduc.showResults(null, null));
    }

    public void updateUserTable() {
        tblUsers.removeAll();
        tblUsers.setModel(cruduc.showResults(null, null));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblUsers = new javax.swing.JTable();
        txtUser = new javax.swing.JTextField();
        spnmRol = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmdSearch = new javax.swing.JButton();
        cmdNewUser = new javax.swing.JButton();
        cmdDelete = new javax.swing.JButton();
        cmdEdit = new javax.swing.JButton();

        tblUsers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Rol", "Usuario"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblUsers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblUsers.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblUsers);

        spnmRol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Administrador", "Administrador de Usuarios", "Secretaria de la Coordinación Curricular", "Asistente de la Coordinación Curricular" }));

        jLabel2.setText("Rol:");

        jLabel3.setText("Usuario:");

        cmdSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/util/img/Search.png"))); // NOI18N
        cmdSearch.setToolTipText("Buscar");
        cmdSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSearchActionPerformed(evt);
            }
        });

        cmdNewUser.setText("Crear nuevo usuario");
        cmdNewUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNewUserActionPerformed(evt);
            }
        });

        cmdDelete.setText("Eliminar");
        cmdDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDeleteActionPerformed(evt);
            }
        });

        cmdEdit.setText("Editar");
        cmdEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cmdNewUser)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmdEdit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdDelete))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(256, 256, 256)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(spnmRol, 0, 255, Short.MAX_VALUE)
                            .addComponent(jLabel2))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 114, Short.MAX_VALUE))
                            .addComponent(txtUser, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addComponent(cmdSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(spnmRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(cmdSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdNewUser)
                    .addComponent(cmdDelete)
                    .addComponent(cmdEdit)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmdNewUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNewUserActionPerformed
        pnlEditUserPanel = new EditUserPanel(true,null);
        LoginWindow.mf.tbpnlTabs.add(pnlEditUserPanel);
        LoginWindow.mf.tbpnlTabs.setSelectedIndex(LoginWindow.mf.tbpnlTabs.getTabCount() - 1);
        LoginWindow.mf.tbpnlTabs.setTitleAt(LoginWindow.mf.tbpnlTabs.getTabCount() - 1,
                "Crear nuevo usuario");
        LoginWindow.mf.tbpnlTabs.setEnabled(false);
    }//GEN-LAST:event_cmdNewUserActionPerformed

    private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteActionPerformed
        String userName = tblUsers.getValueAt(tblUsers.getSelectedRow(), 1).toString();

        int option = JOptionPane.showConfirmDialog(null, "Realmente desea eliminar al usuario \""
                + userName + "\"", "Confirmar borrado", JOptionPane.YES_NO_OPTION);
        if (option == 0) {
            boolean result = pc.delete(new Person(userName, null, null));
            if (result) {
                JOptionPane.showMessageDialog(null, "El usuario \"" + userName + "\" ha sido borrado",
                        "Borrado exitoso", JOptionPane.INFORMATION_MESSAGE);
                tblUsers.removeAll();
                tblUsers.setModel(cruduc.showResults(null, null));
            } else {
                JOptionPane.showMessageDialog(null, "No se pudo borrar al usuario \"" + userName
                        + "\"", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_cmdDeleteActionPerformed

    private void cmdEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEditActionPerformed
        pnlEditUserPanel = new EditUserPanel(false,
                tblUsers.getValueAt(tblUsers.getSelectedRow(), 1).toString());
        LoginWindow.mf.tbpnlTabs.add(pnlEditUserPanel);
        LoginWindow.mf.tbpnlTabs.setSelectedIndex(LoginWindow.mf.tbpnlTabs.getTabCount() - 1);
        LoginWindow.mf.tbpnlTabs.setTitleAt(LoginWindow.mf.tbpnlTabs.getTabCount() - 1,
                "Editar usuario");
        LoginWindow.mf.tbpnlTabs.setEnabled(false);
    }//GEN-LAST:event_cmdEditActionPerformed

    private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchActionPerformed
        if (!txtUser.getText().equals("")) {
            tblUsers.removeAll();
            tblUsers.setModel(cruduc.showResults(txtUser.getText().toLowerCase(), null));
        } else {
            String role;
            role = spnmRol.getSelectedItem().toString();
            tblUsers.removeAll();
            if (role.equals("Administrador")) {
                tblUsers.setModel(cruduc.showResults(null,
                        Person.ADMINISTRATOR));
            } else if (role.equals("Administrador de Usuarios")) {
                tblUsers.setModel(cruduc.showResults(null,
                        Person.USERS_ADMINISTRATOR));
            } else if (role.equals("Secretaria de la Coordinación Curricular")) {
                tblUsers.setModel(cruduc.showResults(null,
                        Person.SEC_COO));
            } else if (role.equals("Todos")) {
                tblUsers.setModel(cruduc.showResults(null, null));
            } else {
                tblUsers.setModel(cruduc.showResults(null,
                        Person.ASIS_COO));
            }
        }
    }//GEN-LAST:event_cmdSearchActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdDelete;
    private javax.swing.JButton cmdEdit;
    private javax.swing.JButton cmdNewUser;
    private javax.swing.JButton cmdSearch;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox spnmRol;
    public static javax.swing.JTable tblUsers;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
}
